package com.czf.testmodule.controller;

import cn.hutool.core.map.MapUtil;
import com.czf.testmodule.service.ParamService;
import com.czf.testmodule.service.UpLoadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author zfChen
 * @create 2022/3/10 13:24
 */
@RestController
@Slf4j
public class UpLoadController {

    @Autowired
    private UpLoadService upLoadService;

    @Autowired
    private ParamService paramService;

    /**
     * 扫描文件，将insert语句转化为select语句，查询数据库进行验证
     * @param sqlFile
     * @return
     */
    @RequestMapping("/transformToSelect")
    public List<String> transformToSelect(@RequestPart("sqlFile") MultipartFile sqlFile) {
        List<String> list = new ArrayList<>();
        HashMap<Object, Object> map = new HashMap<>();
        //从文件中获取insert语句
        List<String> sqls = upLoadService.getSql(sqlFile);

        for (int i = 0; i < sqls.size(); i++) {
            // 获得字段和值的map
            HashMap<String, Object> insertMap = upLoadService.getInsertMap(sqls.get(i));
            // 得到拼接好的select语句并查询
            Map<String, Object> sqlMap = upLoadService.splitSql(insertMap);
            if (MapUtil.isEmpty(sqlMap)) {
                log.info("出现问题的sql={}",sqls.get(i));
                list.add(sqls.get(i));
                continue;
            }
        }
        return list;
    }

    /**
     * 扫描文件，得到文件中的参数和值，并加上前缀或后缀
     * @param paramFile
     * @param prefix
     * @param suffix
     * @return
     */
    @RequestMapping("/scanFile")
    public Map<String, String> scanFile(@RequestPart("paramFile") MultipartFile paramFile,
                                        @RequestParam(value = "prefix",required = false,defaultValue = "") String prefix,
                                        @RequestParam(value = "suffix",required = false,defaultValue = "") String suffix) {
        Map<String, String> map = paramService.scanFile(paramFile, prefix, suffix);
        return map;
    }


    /**
     * 将参数文件和insert初始数据文件进行比较
     * @param sqlFile
     * @param paramFile
     * @param prefix
     * @param suffix
     * @return
     */
    @RequestMapping("/compareFile")
    public List<Object> compareFile(
            @RequestPart("sqlFile") MultipartFile sqlFile,
            @RequestPart("paramFile") MultipartFile paramFile,
            @RequestParam(value = "prefix",required = false,defaultValue = "") String prefix,
            @RequestParam(value = "suffix",required = false,defaultValue = "") String suffix){
        List<String> list = new ArrayList<>();
        HashMap<Object, Object> sqlMap = new HashMap<>();

        List<String> sqls = upLoadService.getSql(sqlFile);
        for (int i = 0; i < sqls.size(); i++) {
            HashMap<String, Object> insertMap = upLoadService.getInsertMap(sqls.get(i));
            if (!StringUtils.isEmpty(insertMap.get("detail_key"))) {
                sqlMap.put(insertMap.get("detail_key"), insertMap.get("detail_value"));
            } else {
                sqlMap.put(insertMap.get("param_key"), insertMap.get("param_value"));
            }
        }
        Map<String, String> paramMap = paramService.scanFile(paramFile,prefix,suffix);

        List<Object> compareMap = paramService.compareMap(sqlMap, paramMap);
        return compareMap;
    }


    @RequestMapping("/test")
    public String method(){
        log.info("测试部署");
        return "测试部署";
    }

    @RequestMapping("/test2")
    public String method2(){
        log.info("测试部署2");

        return "测试部署2";
    }
}
